package com.gitee.wsl.struct.attribute


data class Attribute(var id:Long, var value:String, var defaultValue:String="", val isListValue:Boolean=false){

    fun getDefaultAttributeValue():AbstractAttributeValue<*>{
        if(isListValue)
            return AttributeListValue(id, mutableMapOf(id to defaultValue))
        return AttributeValue(id, defaultValue)
    }

    fun setAttributeValue(value:String):AbstractAttributeValue<*>{
        if(isListValue)
            return AttributeListValue(id, mutableMapOf(id to value))
        return AttributeValue(id, value)
    }

    fun setAttributeValue(vararg values:String):AttributeListValue{
        return AttributeListValue(id, values.associateBy( {id} , {it} ).toMutableMap())
    }

}


data class AttributeSet(var id: Long,var attributeList:MutableList<Attribute>){

    fun getDefaultAttributeSetValue():AttributeSetValue{
        val mapValue=mutableMapOf<Long,AttributeValue>()
        val mapValueList=mutableMapOf<Long,AttributeListValue>()
        attributeList.forEach {
             when(val value=it.getDefaultAttributeValue()){
                 is AttributeValue->{ mapValue[it.id]=value}
                 is AttributeListValue->{mapValueList[it.id]=value}
             }
        }
        return AttributeSetValue(id, mapValue,mapValueList)
    }

}


open class AbstractAttributeValue<T>(open var refId:Long, open var valMap:T)

//用 map 有利于 search


data class AttributeValue(override var refId:Long, override var valMap:MutableMap<Long,String>):AbstractAttributeValue<MutableMap<Long,String>>(refId, valMap){
    constructor(id:Long, value: String) : this(id, mutableMapOf(id to value))

    fun getValue():String=valMap[refId]!!

    fun setValue( value:String){
        valMap[refId]=value
    }
}

data class AttributeListValue(override var refId:Long, override var valMap:MutableMap<Long,String>):AbstractAttributeValue<MutableMap<Long,String>>(refId, valMap){
    constructor(id: Long,vararg valueList:String):this(id,valueList.associateBy( {id} , {it} ).toMutableMap())

    fun getValue():List<String> {
        return valMap.values.toList()
    }

    fun setValue(vararg valueList:String){
        valueList.forEach {
            valMap[refId]=it
        }
    }

}


data class AttributeSetValue(var id:Long, var valMap:MutableMap<Long,AttributeValue>, var listValMap:MutableMap<Long,AttributeListValue>){

    fun getAttributeValueList():List<AbstractAttributeValue<*>>{
        val attributeSetValueList= mutableListOf<AbstractAttributeValue<*>>()
        attributeSetValueList.addAll(valMap.values.toList())
        attributeSetValueList.addAll(listValMap.values.toList())
        return attributeSetValueList
    }

    fun getAttributeValue(attributeId:Long):AbstractAttributeValue<*>?{
       return valMap[attributeId]?:listValMap[attributeId]
    }
}

//class AttributeValueEntity(var id:Long,var refId:Long,)